<?php


namespace app\teacher\controller;

use think\Controller;
use think\Session;

require_once "gtSdk/class.geetestlib.php";
require_once "gtSdk/config.php";

class Captcha extends Controller
{
    public function init()
    {
        $GtSdk = new \GeetestLib(CAPTCHA_ID, PRIVATE_KEY);
        $user_id = uniqid();
        $status = $GtSdk->pre_process($user_id);
        Session::set('gtserver', $status);
        Session::set('user_id', $user_id);
        return $GtSdk->get_response_str();
    }

    public function check($geetest_challenge = '', $geetest_validate = '', $geetest_seccode = '')
    {
        $user_id = Session::get('user_id');
        $GtSdk = new \GeetestLib(CAPTCHA_ID, PRIVATE_KEY);
        if (Session::get('gtserver') == 1) {   //服务器正常
            $result = $GtSdk->success_validate($geetest_challenge, $geetest_validate, $geetest_seccode, $user_id);
            if ($result) {
                echo '{"status":"success"}';
            } else {
                echo '{"status":"fail"}';
            }
        } else {  //服务器宕机,走failback模式
            if ($GtSdk->fail_validate($geetest_challenge, $geetest_validate, $geetest_seccode)) {
                echo '{"status":"success"}';
            } else {
                echo '{"status":"fail"}';
            }
        }
    }
}